package com.boot.bloomFilter.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: lkz
 * @Title: BloomFilterConfig
 * @Description: TODO
 * @Date: 2023/9/15 16:59
 */
@Component
public class BloomFilterConfig {


    @Resource
    RedissonClient redissonClient;

    /**
     * 创建布隆过滤器
     * @param filterName 过滤器名称
     * @param expectedInsertions 预测插入数量
     * @param falseProbability 误判率
     * @param <T>
     * @return
     *
     *  // 预期插入数量
     * long expectedInsertions = 10000L;
     * // 错误比率
     *   double falseProbability = 0.01;
     */
    public <T> RBloomFilter<T> create(String filterName, long expectedInsertions, double falseProbability) {
        RBloomFilter<T> bloomFilter = redissonClient.getBloomFilter(filterName);
        bloomFilter.tryInit(expectedInsertions, falseProbability);
        return bloomFilter;
    }

}
